#include "stm32.h"
#include "ui_stm32.h"

Stm32::Stm32(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Stm32)
{
    ui->setupUi(this);

    ui->led_humi->key=304;
    ui->led_humi->type=1;
    ui->led_humi->val.b_val=0;

    ui->fan->key=303;
    ui->fan->type=1;
    ui->fan->val.b_val=0;
    ui->led_pwm->key=305;
    ui->led_pwm->type=1;
    ui->led_pwm->val.b_val=0;
    ui->humi->key=302;
    ui->led_pwm->type=3;
    ui->led_pwm->val.f_val=0;
    connect(ui->led_humi,SIGNAL(clicked(bool)),SLOT(send(bool)));
    connect(ui->fan,SIGNAL(clicked(bool)),SLOT(send(bool)));
    connect(ui->led_pwm,SIGNAL(clicked(bool)),SLOT(send(bool)));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(shuaxin()));
    connect(ui->temp,SIGNAL(clicked()),this,SLOT(history()));
    connect(ui->humi,SIGNAL(clicked()),this,SLOT(history()));


}

Stm32::~Stm32()
{
    delete ui;
}

void Stm32::send(bool val)
{
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    if(ui->led_humi==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->led_humi->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->led_humi->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    } else if(ui->fan==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->fan->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->fan->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    } else if(ui->led_pwm==sender())
    {
        json.insert("type",2);
        djson.insert("key",ui->led_pwm->key);
        QString sval= QString::number(val);
        djson.insert("val",sval);
        ui->led_pwm->val.b_val=val;
        json.insert("data",QJsonValue(djson));
    }
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    //发布
    GlobalVal ::mqtt->publish(message);

}

void Stm32::dataUpSlot(QByteArray arry)
{

    QString sjson(arry);
    qDebug()<< sjson;
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(sjson.toLatin1(), &jsonError);
    if (!doucment.isNull() && jsonError.error == QJsonParseError::NoError)
    {
        if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
        {
            if (doucment.isObject())
            {
                QJsonObject object = doucment.object();
                if (object.contains("type"))
                {  // 包含指定的 key
                    QJsonValue value = object.value("type");
                    if (value.isDouble())
                    {
                        type=value.toVariant().toInt();
                        qDebug() << "type : " << type;
                    }
                }
                if(type ==1)
                {
                    if (object.contains("result"))
                    {
                        QJsonValue value = object.value("result");
                        if (value.isDouble())
                        {
                            result = value.toVariant().toInt();
                            qDebug() << "result : " << result;
                        }
                    }
                    if(result==0)
                    {
                        if(object.contains("data"))
                        {
                            QJsonValue value = object.value("data");
                            if(value.isArray())
                            {
                                QJsonArray arr=value.toArray();
                                int nsize=arr.size();
                                for(int i=0;i<nsize;i++)
                                {

                                    QJsonValue value=arr.at(i);
                                    if (value.isObject())
                                    {
                                        QJsonObject obj=value.toObject();
                                        if(obj.contains("key"))
                                        {
                                            QJsonValue value = obj.value("key");
                                            qDebug() << value.toInt();
                                            switch (value.toInt()) {
                                            case 301:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    ui->humi_x->setText(value.toString()+"℃");

                                                }
                                                break;
                                            case 302:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    ui->temp_x->setText(value.toString()+"%");

                                                }
                                                break;
                                            case 303:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    QString text=value.toString();
                                                    int val=text.toInt();
                                                    ui->fan->setChecked(val);

                                                }
                                                break;
                                            case 304:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    QString text=value.toString();
                                                    int val=text.toInt();
                                                    ui->led_humi->setChecked(val);

                                                }
                                                break;
                                            case 305:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    QString text=value.toString();
                                                    int val=text.toInt();
                                                    ui->led_pwm->setChecked(val);

                                                }
                                                break;
                                            case 306:
                                                if(obj.contains("val"))
                                                {
                                                    QJsonValue value = obj.value("val");
                                                    //QString text=value.toDouble();
                                                    QString text=value.toString();
                                                    int val=text.toInt();
                                                    ui->fire->setChecked(val);

                                                }
                                                break;
                                            default:
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }else
                    {
                        qDebug() <<"result错误";
                    }
                }

            }
        }
    }
}

void Stm32::shuaxin()
{
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    json.insert("type",1);
    json.insert("limit","all");
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    //发布
    GlobalVal ::mqtt->publish(message);
}

void Stm32::history()
{
//    GlobalVal::mqtt;
    QDateTime currentTime = QDateTime::currentDateTimeUtc();
    qint64 timestamp = currentTime.toMSecsSinceEpoch()/ 1000;
    qint64 times=timestamp-60;
    qDebug() << "Seconds since epoch:" << timestamp;
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    QJsonObject json;
    QJsonObject djson;
    json.insert("type",4);
    QJsonArray jsonarr;
    jsonarr.append(times);
    jsonarr.append(timestamp);
    djson.insert("limit",QJsonValue(jsonarr));
    json.insert("data",QJsonValue(djson));

    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Compact);
    message.setPayload(byte_array);
    //发布
    his= new History;
    QObject::connect(GlobalVal::mqtt, SIGNAL(dataUpSignal(QByteArray)),
                      his, SLOT(dataUpSlot(QByteArray)));
    his->show();
    GlobalVal ::mqtt->publish(message);


}
